package com.groups.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import com.groups.common.SPageBean;
import com.groups.dao.ICommonDAO;
import com.groups.entity.Good;
import com.groups.entity.Order;

public class GoodInfoService {
	private ICommonDAO commonDao;
	
	public ICommonDAO getCommonDao() {
		return commonDao;
	}

	public void setCommonDao(ICommonDAO commonDao) {
		this.commonDao = commonDao;
	}
	public int getOrderCount(int goodId){
		String hql = "from Order o inner join fetch Good where o.good.goodId=?";
		Object[] args = new Object[]{goodId};
		List list = commonDao.list(hql, args);
		int count = list.size();
		return count;
	}
	public List<Object[]> getUpdateInfo(){
	    List<Object[]> goods = new ArrayList<Object[]>();
		String hql = "from Good";
		List<Good> list = commonDao.list(hql);
		int count = 0;
		for(Good g : list){
			boolean flag = g.getEndTime().after(new Date());
			if(g.getStartTime().before(new Date())&&g.getEndTime().after(new Date())){
				String hql1 = "from Order o where o.good.goodId = %d";
				hql1 = String.format(hql1, g.getGoodId());
				Object[] args = new Object[]{g.getGoodId()};
				List list1 = commonDao.list(hql);
				count = list1.size();
				Object[] o = new Object[]{g.getGoodId(),g.getGoodName(),g.getGoodPrice(),
				 g.getGoodOldprice(),g.getGoodDiscount(),g.getGoodSave(),g.getStartTime(),
				 g.getEndTime(),g.getGoodkind(),g.getGoodUrl(),g.getGoodInfo(),count};
				goods.add(o);
				 }
			}
		return goods;
	}
	public Object[] getGoodDetail(int goodId){
		String hql = "from Order o inner join fetch o.good where o.good.goodId=?";
		Object[] args = new Object[]{goodId};
		List list = commonDao.list(hql, args);
		int count = list.size();
		//Order order = (Order)list.get(0);
		String hql1 = "from Good g where g.goodId = ?";
		List list1 = commonDao.list(hql1,args);
		Good g = (Good) list1.get(0);
		Object[] o = new Object[]{g.getGoodName(),g.getGoodOldprice(),g.getGoodSave(),g.getGoodPrice(),
				g.getGoodDiscount(),g.getStartTime(),g.getEndTime(),
				g.getGoodInfo(),g.getGoodUrl(),g.getGoodkind(),count,goodId};
		return o;
	}
	public List getGoodsByPage(SPageBean spb,int goodkind){
		String hql = "from Good g where now() between g.startTime and g.endTime";
		if(goodkind!=0){
			  hql += " and g.goodkind = '"+goodkind+"'";
		}
		commonDao.listByPage(hql, spb);
		return (List)spb.getDatas();
	}
	public int madeOrderId(){
		int[] data1 = {0,1,2,3,4,5,6,7,8,9};
		Random r = new  Random();
		StringBuffer str1= new StringBuffer();
		for(int i=0;i<5;i++){
			str1.append(data1[r.nextInt(10)]);
        }
		String str = str1.toString();
		int id = Integer.parseInt(str);
		return id;
	}
	public void addOrder(Order order){
			commonDao.add(order);	
	}
	public Good getGoodById(int goodId){
		String hql = "from Good g where g.goodId = ?";
		Object[] args = new Object[]{goodId};
		List list = commonDao.list(hql, args);
		Good g = (Good)list.get(0);
		return g;
	}
}
